import {createRouter, createWebHistory,createWebHashHistory} from 'vue-router'
import type { RouterOptions } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/admin/page/UserLogin.vue'),
    },
    {
      path: '/home',
      name: 'home',
      redirect:'control',
      component: () => import('../views/admin/GlobalContainer.vue'),
      children:[
        {
          path: '/control',
          name: 'control',
          component: () => import('@/views/admin/page/ConsoleOutput.vue'),
        },
        {
          path: '/user',
          name: 'user',
          component: () => import('@/views/admin/page/UserManagement.vue'),
        },
        {
          path: '/menu',
          name: 'menu',
          component: () => import('@/views/admin/page/MenuManagement.vue'),
        },
        {
          path: '/system',
          name: 'system',
          component: () => import('@/views/admin/page/SystemSetting.vue'),
        },
        {
          path: '/billing',
          name: 'billing',
          component: () => import('@/views/admin/page/BillingManagement.vue'),
        },
        {
          path:"/roleMenu",
          name:"roleMenu",
          component: () => import('@/views/admin/page/RoleManagement.vue')
        },
        {
          path: '/settings',
          name: 'settings',
          component: () => import('@/views/admin/settings/UserSettings.vue')
        },
        {
          path: '/chat',
          name: 'chat',
          component: () => import('@/views/admin/chat/ChatDeepSeek.vue')
        },
        {
          path: '/chat.do',
          name: 'chat2',
          //component: () => import('@/components/cs/ChatContainer.vue'),
          component: () => import('@/views/admin/chat/Chat.vue'),
          // children:[
          //   {
          //     path: ':sessionId',
          //     name: 'chat-session',
          //     component: () => import('@/components/cs/ChatHistory.vue')
          //   }
          // ]
        }
      ]
    },
    {
      path: '/404',
      name: '404',
      component: () => import('../views/admin/page/404.vue'),
    },
  ]
} as RouterOptions)

router.beforeResolve(async (to, from, next) => {

  const isFrontPage = to.matched.some(record => record.path.startsWith('/home'))
  // 获取认证状态
  const isAuthenticated = localStorage.getItem('authToken');


  if (isFrontPage){
    if (!isAuthenticated) {
      next({
        path: '/',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next()
  }
});

// // 注意：这里你有两个相同的 beforeResolve 守卫，应该只保留一个并合并它们的逻辑。
// // 全局解析守卫通常用于处理一些在确认导航后但在完成之前需要做的事情，
// // 比如加载数据或初始化某些东西。如果你不需要这样的功能，可以移除它。
//
// // 全局后置钩子：在导航完成后触发
// router.afterEach((to, from) => {
//   // 这里可以放置一些清理工作或分析代码
// });

export default router
